package game.windows;

import java.util.ArrayList;

import game.shared.Constants;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Items extends Button{

	protected int way;//Item nằm ở slot bên trái hoặc bên phải.
	protected int storeSlotCol;
	protected int storeSlotRow;
	protected int costBuy; //Gia mua
	protected int costSell; //Gia ban
	protected int quantity; // 10hp, 10mana or (number_donvi)
	private ArrayList<WindowControl> listItems;

	public Items(ArrayList<WindowControl> listItems, Items item, int way, int idGroup) {
		super(item);
		this.functionCode = item.functionCode;
		this.way = way;
		this.idGroup = idGroup;
		this.listItems = listItems;
		storeItemToLeft();
		this.id = ++Constants.ID_ITEMS;
		this.costBuy = item.costBuy;
		this.costSell = item.costSell;
		calXY();
		this.quantity = item.quantity;
	}
	
//	public Items(Items item, int way, int idGroup, int storeSlotCol, int storeSlotRow) {
//		super(item);
//		this.functionCode = item.functionCode;
//		this.way = way;
//		this.idGroup = idGroup;
//		this.storeSlotCol = storeSlotCol;
//		this.storeSlotRow = storeSlotRow;
//		this.id = ++Constants.ID_ITEMS;
//		calXY();
//	}

	public Items(ArrayList<WindowControl> listItems, int tab, int way, int storeSlotCol, int storeSlotRow, 
			int functionCode, TextureRegion tButtonNomal, TextureRegion tButtonPressed, int costBuy, 
			int costSell, int quantity) {
		super(functionCode, tButtonNomal, tButtonPressed);
		this.way = way;
		this.storeSlotCol = storeSlotCol;
		this.storeSlotRow = storeSlotRow;
		id = ++Constants.ID_ITEMS;
		this.idGroup = tab;//
		calXY();
		this.costBuy = costBuy;
		this.costSell = costSell;
		this.listItems = listItems;
		this.quantity = quantity;
	}
	
	@Override
	public void update() {
		super.update();
		if (pressed)
			Constants.ID_ITEMS_CHOOSE = this.id;
	}

	@Override
	public void render(SpriteBatch sb) {
		// TODO Auto-generated method stub
		super.render(sb);
	}
	
	private void storeItemToLeft(){
		for (int j=0; j<=Constants.NUMBER_SLOT_LEFT_ROW - 1; j++)
			for (int i=0; i<=Constants.NUMBER_SLOT_LEFT_COL - 1; i++){
				boolean slotNull = true;
				for(WindowControl w:listItems){
					if (w instanceof Items)
						if ((((Items) w).storeSlotCol == i)&(((Items) w).storeSlotRow == j)&(((Items) w).way == 0))
						slotNull = false;
				}
				if(slotNull == true){
					this.storeSlotCol = i;
					this.storeSlotRow = j;
					i = Constants.NUMBER_SLOT_LEFT_COL - 1;
					j = Constants.NUMBER_SLOT_LEFT_ROW - 1;
				}
			}
	}
	
	private void calXY(){
		switch (this.way) {
		case 0://slot trai
			this.x = Constants.DISTANCE_SLOT_X_0_LEFT - this.width / 2 + Constants.DISTANCE_SLOT_BUNKER * storeSlotCol;
			this.y = Constants.DISTANCE_SLOT_Y_0 - Constants.DISTANCE_SLOT_BUNKER * storeSlotRow;
			break;
		case 1://thuoc slot phai
			this.x = Constants.DISTANCE_SLOT_X_0 - this.width / 2 + Constants.DISTANCE_SLOT_BUNKER * storeSlotCol;
			this.y = Constants.DISTANCE_SLOT_Y_0 - Constants.DISTANCE_SLOT_BUNKER * storeSlotRow;
			break;
		case 2://tab
			this.x = Constants.DISTANCE_SLOT_X_0 - this.width / 2 + Constants.DISTANCE_SLOT_BUNKER * storeSlotCol;
			this.y = Constants.DISTANCE_SLOT_Y_0 - Constants.DISTANCE_SLOT_BUNKER * storeSlotRow;
			break;
		default:
			System.out.println("Items khong thuoc ruong do nao ca!!!");
			break;
		}
	}
	public void actionWhenBuy(){
		boolean check = true;
		if (checkSlotLeftFull() == false)
			check = false;
		if (checkGold() == false)
			check = false;
		if (checkParameterNomalAndMax() == false)
			check = false;
		if (check == true){
			switch (this.idGroup) {
			case 1:
				Constants.HP += this.quantity;
				Constants.GOLD -= this.costBuy;
				break;
			case 2:
				Constants.MANA += this.quantity;
				Constants.GOLD -= this.costBuy;
				break;
			case 3:
				Constants.HP_MAX += this.quantity;
				Constants.GOLD -= this.costBuy;
				break;
			case 4:
				Constants.MANA_MAX += this.quantity;
				Constants.GOLD -= this.costBuy;
				break;
			}
				
			if ((this.idGroup != 1) && (this.idGroup != 2)){//Neu item ko phai la binh mau,binh mana.
				Items item = new Items(listItems, this, 0, 0);
				listItems.add(item);
			}
		}
	}
	
	public void actionWhenSell(){
		if (this.way == 0){
			Constants.GOLD += this.costSell;
			switch (this.functionCode) {
			case Constants.FUNCTION_CODE_HP_1:
				Constants.HP -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_HP_2:
				Constants.HP -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_HP_3:
				Constants.HP -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_HP_4:
				Constants.HP -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_MANA_1:
				Constants.MANA -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_MANA_2:
				Constants.MANA -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_MANA_3:
				Constants.MANA -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_MANA_4:
				Constants.MANA -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_RED_1:
				Constants.HP_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_RED_2:
				Constants.HP_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_RED_3:
				Constants.HP_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_RED_4:
				Constants.HP_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_BLUE_1:
				Constants.MANA_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_BLUE_2:
				Constants.MANA_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_BLUE_3:
				Constants.MANA_MAX -= this.quantity;
				break;
			case Constants.FUNCTION_CODE_GEM_BLUE_4:
				Constants.MANA_MAX -= this.quantity;
				break;
			}
			for (int i=0; i< listItems.size(); i++)
				if (listItems.get(i).id == this.id)
					listItems.remove(i);
		}
	}
	
	private boolean checkSlotLeftFull(){
		Items tempItem;
		int numberSlotFull = 0;
		for (int i=0; i< listItems.size(); i++)
			if (listItems.get(i) instanceof Items){
				tempItem = (Items) listItems.get(i);
				if (tempItem.way == 0)
					numberSlotFull++;
			}
		if (numberSlotFull > Constants.NUMBER_SLOT_LEFT_COL * Constants.NUMBER_SLOT_LEFT_ROW){
			Bunker.information = "Warning : Slots full.";
			return false;
		}
		else return true;
	}
	private boolean checkGold(){
		if (Constants.GOLD < this.costBuy){
			Bunker.information = "Warning : Not enough gold.";
			return false;
		}
		else return true;
	}
	private boolean checkParameterNomalAndMax(){
		switch (this.idGroup) {
		case 1:
			if (this.quantity + Constants.HP > Constants.HP_MAX){
				Bunker.information = "Warning : Hp > hp_max.";
				return false;
			}
			break;
		case 2:
			if (this.quantity + Constants.MANA > Constants.MANA_MAX){
				Bunker.information = "Warning : Mana > mana_max.";
				return false;
			}
			break;
		}
		return true;
	}
}
